<?php

/**
* https://gitee.com/swoole/swoole_framework
*
*
*/

require './QueueHelper.php';

$server = new swoole_websocket_server("0.0.0.0", 9502);

function getKey($party_id)
{
    return 'PARTY_CHAT_'.$party_id;
}

$server->on('open', function($server, $req) {
    var_dump($req->get);
    // 行会的聊天室里面加入 socket FD标识
    QueueHelper::getRedis()->setSet(getKey($req->get['party']), $req->fd);
    echo "用户-".$req->fd."-上线\n";
});

$server->on('message', function($server, $frame) {
    echo "received message: {$frame->data}\n";

    // json 数据传入party_id 和 聊天内容
    var_dump($frame->data);
    // 聊天内容插入 list

    // 发消息的时候判断 收到的信息然后获取指定行会的会员FD 然后遍历PUSH消息


    $server->push($frame->fd, json_encode(["hello", "world"]));
});

$server->on('close', function($server, $fd) {
    echo '用户-'.$fd."-下线\n";
});

$server->start();
